import React, { useState, useRef } from 'react';
import { Modal, Button, Input, Form } from 'antd';
import http from '../../../services/http';

const { TextArea } = Input;

const Edit = (props) => {
    const [visible, setVisible] = useState(false);
    
    let form = useRef();
    const editbtn = () => {
        http({
            url: '/findstandard',
            data: {
                stan_id: props.edit
            }
        }).then((res) => {
            if (res.length) {
                setVisible(true);
                form.current.setFieldsValue({
                    stan_name: res[0].stan_name,
                    stan_unild: res[0].stan_unild,
                    stan_standardUnit: res[0].stan_standardUnit,
                    stan_upper: res[0].stan_upper,
                    stan_lower: res[0].stan_lower,
                    stan_normalMsg: res[0].stan_normalMsg,
                    stan_upMsg: res[0].stan_upMsg,
                    stan_lowerMsg: res[0].stan_lowerMsg
                })
            }
        })
    }
    return (
        <>
            <Button style={{ border: 'none' }} size={'small'} onClick={editbtn}>
                编辑
            </Button>
            <Modal
                title="编辑体检指标"
                centered
                forceRender={true}
                visible={visible}
                onOk={() => {
                    console.log(form.current.getFieldValue())
                    http({
                        url: '/editstandard',
                        method: 'post',
                        data: {
                            stan_unild: form.current.getFieldValue().stan_unild,
                            stan_name: form.current.getFieldValue().stan_name,
                            stan_standardUnit: form.current.getFieldValue().stan_standardUnit,
                            stan_upper: form.current.getFieldValue().stan_upper,
                            stan_lower: form.current.getFieldValue().stan_lower,
                            stan_normalMsg: form.current.getFieldValue().stan_normalMsg,
                            stan_upMsg: form.current.getFieldValue().stan_upMsg,
                            stan_lowerMsg: form.current.getFieldValue().stan_lowerMsg,
                            stan_id: props.edit
                        }
                    }).then((res) => {
                        console.log(res)
                            window.location.reload();
                    })
                }}
                onCancel={() => setVisible(false)}
                width={800}
            >

                <Form ref={form}
                    labelCol={{ span: 5, }}
                >
                    <Form.Item label="标准指标名称" name="stan_name" rules={[{ required: true, },]}>
                        <Input />
                    </Form.Item>
                    <Form.Item label="套餐ID" name="stan_unild" rules={[{required: true, message: '套餐ID仅支持数字类型'  },]}>
                        <Input/>
                    </Form.Item>
                    <Form.Item label="指标单位" name="stan_standardUnit" rules={[{ required: true, },]}>
                        <Input />
                    </Form.Item>
                    <Form.Item label="参考上限" name="stan_upper" rules={[{ required: true, },]}>
                        <Input />
                    </Form.Item>
                    <Form.Item label="参考下限" name="stan_lower" rules={[{ required: true, },]}>
                        <Input />
                    </Form.Item>

                    <Form.Item label="正常提示" name="stan_normalMsg" rules={[{ required: true, },]}>
                        <TextArea rows={4} />
                    </Form.Item>

                    <Form.Item label="偏高提示" name="stan_upMsg" rules={[{ required: true, },]}>
                        <TextArea rows={4} />
                    </Form.Item>

                    <Form.Item label="偏低提示" name="stan_lowerMsg" rules={[{ required: true, },]}>
                        <TextArea rows={4} />
                    </Form.Item>
                </Form>

            </Modal>
        </>
    );
};

export default Edit;